
package control.servlet;

import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import bean.CharacterBEAN;
import bean.MailBEAN;
import dao.CharacterDAO;
import dao.MailDAO;

/**
 * Servlet implementation class Mail
 */
public class Mail extends HttpServlet {
	private static final long	serialVersionUID	= 1L;
	private CharacterDAO		characterDAO;
	private MailDAO				mailDAO;

	public void init( ServletConfig config ) throws ServletException {
		characterDAO = (CharacterDAO) config.getServletContext().getAttribute(
				"CharacterDAO" );
		mailDAO = (MailDAO) config.getServletContext().getAttribute( "MailDAO" );
	}

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public Mail() {
		super();
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet( HttpServletRequest request,
			HttpServletResponse response ) throws ServletException, IOException {
		String action = request.getParameter( "action" );
		HttpSession session = request.getSession();
		if( session.getAttribute( "ID" ) != null ) {
			int accountID = (Integer) session.getAttribute( "ID" );
			CharacterBEAN characterBEAN = characterDAO
					.getCharacterByID( accountID );
			request.setAttribute( "character", characterBEAN );
			if( action.startsWith( "getMail" ) ) {
				int mailID = Integer.parseInt( action.substring( 7 ) );
				MailBEAN mail = mailDAO.getMailByID( mailID );
				mailDAO.setMailRead( mail.getMailID() );
				request.setAttribute( "mail", mail );
				goTo( "WEB-INF/logged/showMail.jsp", request, response );
			}
		} else {
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost( HttpServletRequest request,
			HttpServletResponse response ) throws ServletException, IOException {
		String action = request.getParameter( "action" );
		System.out.println( action );
		HttpSession session = request.getSession();
		if( session.getAttribute( "ID" ) != null ) {
			int accountID = (Integer) session.getAttribute( "ID" );
			CharacterBEAN characterBEAN = characterDAO
					.getCharacterByID( accountID );
			request.setAttribute( "character", characterBEAN );
			if( action.equals( "replyMail" ) ) {
				MailBEAN oldMail = mailDAO.getMailByID( Integer
						.parseInt( request.getParameter( "oldMailID" ) ) );
				mailDAO.addMail( characterBEAN.getCharacterID(), oldMail
						.getMailFromID(), "RE:" + oldMail.getTitle(), request
						.getParameter( "textArea" ) );
				request.setAttribute( "mailList", mailDAO
						.getMailsByID( characterBEAN.getCharacterID() ) );
				goTo( "WEB-INF/logged/mailBox.jsp", request, response );
				return;
			} else if( action.equals( "removeMail" ) ) {
				MailBEAN oldMail = mailDAO.getMailByID( Integer
						.parseInt( request.getParameter( "oldMailID" ) ) );
				mailDAO.removeMail( oldMail.getMailID() );
				request.setAttribute( "mailList", mailDAO
						.getMailsByID( characterBEAN.getCharacterID() ) );
				goTo( "WEB-INF/logged/mailBox.jsp", request, response );
				return;
			} else if( action.equals( "createMail" ) ) {
				goTo( "WEB-INF/logged/sendMail.jsp", request, response );
				return;
			} else if( action.equals( "sendNewMail" ) ) {
				int sendTo = characterDAO.getCharIDByName( request
						.getParameter( "mailTo" ) );
				String title = request.getParameter( "mailTitle" );
				String content = request.getParameter( "textArea" );
				mailDAO.addMail( characterBEAN.getCharacterID(), sendTo, title,
						content );
				request.setAttribute( "mailList", mailDAO
						.getMailsByID( characterBEAN.getCharacterID() ) );
				goTo( "WEB-INF/logged/mailBox.jsp", request, response );
				return;
			} else if( action.equals( "removeMails" ) ) {
				// TODO delete some mails
			} else if( action.equals( "support" ) ) {
				// TODO
				goTo( "WEB-INF/logged/welcome.jsp", request, response );
				return;
			}
		} else {
		}
	}

	private void goTo( String action, HttpServletRequest request,
			HttpServletResponse response ) throws ServletException, IOException {
		try {
			RequestDispatcher dispatcher = request
					.getRequestDispatcher( action );
			dispatcher.forward( request, response );
		} catch( Exception e ) {
			e.printStackTrace();
		}
	}
}
